package studentmanager;

import java.awt.BorderLayout;
import java.awt.Color;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListDataListener;

import structure.Course;
import gui.GUIMain;
import gui.SearchTextField;

public class GUICourseDisplay extends JPanel{
	
	private static final long serialVersionUID = 1L;
	private SearchTextField searchTF;
	private JList<Course> display;
	private CourseModel displayModel;
	
	public GUICourseDisplay() {
		setOpaque(false);
		setLayout(new BorderLayout());
		
		searchTF = new SearchTextField();
		searchTF.getDocument().addDocumentListener(new SearchListener());
		add(searchTF, BorderLayout.NORTH);
		
		displayModel = new CourseModel();
		display = new GUICourseJList(displayModel);
		display.setCellRenderer(new GUICourseCell());
		display.setFixedCellHeight(50);
		display.setForeground(Color.gray);
		JScrollPane pane = new JScrollPane(display);
		pane.setOpaque(false);
		pane.setBorder(new EmptyBorder(2, 0, 2, 0));
		add(pane, BorderLayout.CENTER);
	}
	
	public CourseModel getDisplayModel(){
		return this.displayModel;
	}
	
	public JList<Course> getJList(){
		return this.display;
	}
	
	public void getCourseFromDB(boolean isGettingDone){
		try {
			displayModel.clearCourse();
			for(Course count : GUIMain.getDb().getCoursesByDone(false)){
				displayModel.addCourse(count);
			}
			if(isGettingDone){
				for(Course count : GUIMain.getDb().getCoursesByDone(true)){
					displayModel.addCourse(count);
				}
			}
			displayModel.search(searchTF.getText());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	class SearchListener implements DocumentListener{

		@Override
		public void changedUpdate(DocumentEvent e) {
			search();
		}

		@Override
		public void insertUpdate(DocumentEvent e) {
			search();
		}

		@Override
		public void removeUpdate(DocumentEvent e) {
			search();
		}
		
		public void search(){
			display.clearSelection();
			displayModel.search(searchTF.getText().trim());
		}
	}
	
	class CourseModel implements ListModel<Course>{
		private ArrayList<Course> collection;
		private ArrayList<Course> displayCollection;
		
		public CourseModel() {
			collection = new ArrayList<>();
			displayCollection = new ArrayList<>();
		}
		
		public ArrayList<Course> getCollections(){
			return this.displayCollection;
		}
		
		public void addCourse(Course param){
			collection.add(param);
		}
		
		public void clearCourse(){
			displayCollection.clear();
			collection.clear();
		}
		
		@SuppressWarnings("unchecked")
		public void displayAll(){
			displayCollection = (ArrayList<Course>) collection.clone();
		}
		
		public void search(String param){
			displayCollection.clear();
			if(param.equals("")){
				displayAll();
				display.validate();
				display.repaint();
				return;
			}
			for(Course e : collection){
				if(e.toString().toLowerCase().contains(param.toLowerCase())){
					displayCollection.add(e);
				}
			}
			display.validate();
			display.repaint();
		}
		
		@Override
		public void addListDataListener(ListDataListener l) {}

		@Override
		public Course getElementAt(int index) {
			return displayCollection.get(index);
		}

		@Override
		public int getSize() {
			return displayCollection.size();
		}

		@Override
		public void removeListDataListener(ListDataListener l) {}
	}
	
	
}
